﻿using System;
using System.Linq;
using System.Windows.Forms;
using System.IO;
using System.Collections.Generic;
using Citi.Helpers;
using System.Configuration;

namespace Citi.BOExtraction.WindowsFormUI
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            //var reportName = "PEARL_REPORT_Total_2013_Pos Credicard : 10970721";
            //var url = "https://pearl.nj.ssmb.com/siteminderagent/forms/login.fcc?TYPE=33554433&REALMOID=06-fb51622c-add9-1001-9bdc-847a5366ff21&GUID=&SMAUTHREASON=0&METHOD=GET&SMAGENTNAME=-SM-TSXzCZBCWQfzzR6LEV6XtEMrota02ItSl0QgFPxXin03Wt9NJusCYSasBcT6zdoJT%2fuU6B%2b%2b02sSh2OVblMd8gXfnNQ9TyP9&TARGET=-SM-%2fpsp%2fplpfprg%2fEMPLOYEE%2fEPM%2fc%2fBCC_REPORTS_HOME%2eBCC_BO_R3_LINK%2eGBL%3fFolderPath%3dPORTAL_ROOT_OBJECT%2eBCC_COUNTRY_CORE%2eBCC_REPORTING%2eBCC_BO_R3_LINK_GBL%26IsFolder%3dfalse%26IgnoreParamTempl%3dFolderPath-%2cIsFolder";
            //var userLogin = "LD41319";
            //var pathToSave = @"c:\temp\Pearl\tb_QRY_PEARL_2014.xls";
            //var joinSheets = true;
            //var newSheetName = "tb_QRY_PEARL_2014";

            var arguments = args.ToList();
            var reportName = MainArgument.GetArgumentValue("reportName", arguments, '-');

            try
            {
                var url = MainArgument.GetArgumentValue("url", arguments, '-');
                var boUrl = MainArgument.GetArgumentValue("boUrl", arguments, '-');
                var userLogin = MainArgument.GetArgumentValue("userLogin", arguments, '-');
                var pathToSave = MainArgument.GetArgumentValue("pathToSave", arguments, '-');
                var joinSheets = MainArgument.GetArgumentValue<bool>("joinSheets", arguments, '-');
                var newSheetName = MainArgument.GetArgumentValue("newSheetName", arguments, '-');
                var browser = MainArgument.GetArgumentValue<int>("browser", arguments, '-');
                var parameters = MainArgument.GetArgumentValue("parameters", arguments, '-');
                var newWindowNoAction = MainArgument.GetArgumentValue<bool>("newWindowNoAction", arguments, '-');
                string singleSignOnUrl = null;

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);

                //if (browser == 0)
                //    Application.Run(new BoExtraction(reportName, url, userLogin, pathToSave, joinSheets, newSheetName));
                //else
                Application.Run(new EOBusinessObjectsExtraction(reportName, url, singleSignOnUrl, userLogin, pathToSave, joinSheets, newSheetName, parameters, newWindowNoAction, boUrl));
            }
            catch (Exception e)
            {
                var logPath = string.Format(ConfigurationManager.AppSettings["LogPath"], reportName);
                if (File.Exists(logPath)) File.Delete(logPath);

                var writer = new ConsoleInFile(logPath);
                Console.SetOut(writer);
                Console.WriteLine("#ERROR {0}", e.Message);
            }
        }
    }
}
